#-*- coding: big5 -*-
from modelRepository import modelRepository
from log import log
from cStringIO import StringIO
import re

class ReflectionSerialzeXml:
    def __init__(self):
        self.intIndent=0
        self.stringIo=StringIO()
        self.lstEndTagStack=[]
    def write(self,strContent):
        self.stringIo.write('%s\n'%strContent)
        print strContent
    def saveXml(self):
        book=modelRepository.getModelBook()
        self.write('<Book strComponentName="modelBook" strText="%s" strAuthor="%s">'%(book.strText,book.strAuthor))
        self.constructBookXml(book.lstBookComponent)
        self.write('</Book>')
        open('output.xml','wb').write(self.stringIo.getvalue())
    def getTag(self,obj):
        strTag=re.findall(r'<model\.(.*?) ',str(obj))[0]
        return strTag
    def constructBookXml(self,lstBookComponent):
        if not lstBookComponent: return
        for bookComponent in lstBookComponent:
            if bookComponent.isLeaf():
                self.write(' '*self.intIndent+'<%s strComponentName="%s" strText="%s"/>'%(self.getTag(bookComponent),bookComponent.strComponentName,bookComponent.strText))
            else:
                if bookComponent.strText:
                    self.write(' '*self.intIndent+'<%s strComponentName="%s" strText="%s">'%(self.getTag(bookComponent),bookComponent.strComponentName,bookComponent.strText))
                    self.lstEndTagStack.append(self.getTag(bookComponent))
                self.intIndent+=4
                self.constructBookXml(bookComponent.lstBookComponent)
                self.intIndent-=4
                self.write(' '*self.intIndent+'</%s>'%(self.lstEndTagStack.pop()))
reflectionSerialzeXml=ReflectionSerialzeXml()

import xml.sax.handler
class XmlReader(xml.sax.handler.ContentHandler):
    def __init__(self):
        self.lstBookComponent=[]
        modelRepository.clear()
    def startElement(self,name,attributes):
        strClass=str(name)
        strComponentName=str(attributes['strComponentName'])
        strText=str(attributes['strText'])
        bookComponent=modelRepository.new(strComponentName,strClass,strText)
        if self.lstBookComponent:
            strN1=self.lstBookComponent[-1].strComponentName
            strN2=strComponentName
            print 'strN1:%s,strN2:%s'%(strN1,strN2)
            modelRepository.add(self.lstBookComponent[-1].strComponentName,strComponentName)
        if bookComponent.isLeaf()==False:
            self.lstBookComponent.append(bookComponent)
    def characters(self, data): pass
    def endElement(self, name):
        bookComponent=modelRepository.new('testTypeOnly',name,'')
        if bookComponent.isLeaf()==False:
            self.lstBookComponent.pop()
        
def testLoad():
    parser=xml.sax.make_parser()
    handler=XmlReader()
    parser.setContentHandler(handler)
    parser.parse("output.xml")
    from view import ViewBook
    ViewBook().show()
    
if __name__=='__main__':
    testLoad()
